<template>
  <VPage>
    <VTable ref="tableRef" v-bind="tableOptions">
      <template #form="{ form }">
        <VGroup>
          <el-select v-model="form.input.type" style="width: 130px">
            <el-option label="备料单号" value="plan_no"></el-option>
            <el-option label="备库需求单号" value="demand_no"></el-option>
            <el-option label="采购单号" value="purchase_no"></el-option>
            <el-option label="备料单号/sku" value="plan_no_sku"></el-option>
          </el-select>
          <VBatchInput v-model="form.input.value" placeholder="请输入" class="w-40" @search="query" />
        </VGroup>
        <VGroup>
          <el-select v-model="form.sku.type" class="w-28">
            <el-option label="sku" value="sku"></el-option>
            <el-option label="赛盒SKU" value="sai_he_sku"></el-option>
            <el-option label="自定义sku" value="custom_sku"></el-option>
            <el-option label="ASIN" value="asin"></el-option>
          </el-select>
          <VBatchInput v-model="form.sku.value" placeholder="请输入" class="w-40" @search="query" />
        </VGroup>
        <VGroup>
          <el-select v-model="form.supplier.type" class="w-28">
            <el-option label="供应商ID" value="supplier_id"></el-option>
            <el-option label="供应商名称" value="supplier_name"></el-option>
          </el-select>
          <VBatchInput v-model="form.supplier.value" placeholder="请输入" class="w-40" @search="query" />
        </VGroup>
        <VCascader v-model="form.purchase_user_id" placeholder="采购人员" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.plan_type" :options="options.plan_type" multiple collapse-tags style="min-width: 160px" placeholder="计划类型" />
        <VCascader v-model="form.create_user_id" placeholder="申请人员" multiple collapse-tags style="min-width: 160px" />
        <VCascader v-model="form.director_id" placeholder="供应商负责人" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.country" placeholder="国家" :options="options.country" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.is_combination" :options="options.is_abnormal" multiple collapse-tags style="min-width: 140px" placeholder="是否组合sku" />
        <VSelect v-model="form.plan_status" :options="options.plan_status" multiple collapse-tags style="min-width: 140px" placeholder="备料状态" />
        <VCascader v-model="form.department_name" type="department" multiple collapse-tags style="min-width: 160px" placeholder="备料部门" />
        <VCascader v-model="form.sale_id" multiple collapse-tags style="min-width: 160px" placeholder="销售" />
        <VGroup>
          <div class="v-group-item">余料数</div>
          <el-input-number v-model="form.leave_num.start" placeholder="最小" :controls="false" :min="0" :step="1" :precision="0" class="w-20" />
          <div class="v-group-item">-</div>
          <el-input-number v-model="form.leave_num.end" placeholder="最大" :controls="false" :min="0" :step="1" :precision="0" class="w-20" />
        </VGroup>
        <VSelect v-model="form.platform" :options="options.platform" multiple collapse-tags style="min-width: 160px" placeholder="平台" />
        <VGroup>
          <el-select v-model="form.new.type" style="width: 130px">
            <el-option label="是否新品" value="is_new"></el-option>
            <el-option label="是否特采" value="is_special_purchase"></el-option>
          </el-select>
          <VSelect v-model="form.new.value" placeholder="请选择" :options="options.is_new" multiple collapse-tags style="min-width: 160px" />
        </VGroup>
        <VSelect v-model="form.is_dangerous" placeholder="是否危险品" :options="options.is_dangerous" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.is_change" placeholder="是否有变更" :options="options.is_change" multiple collapse-tags style="min-width: 160px" />
        <VSelect v-model="form.is_contract" placeholder="是否有备料合同" :options="options.is_contract" multiple collapse-tags style="min-width: 160px" />
        <VGroup>
          <el-select v-model="form.dateRange.type" style="width: 130px">
            <el-option label="申请日期" value="create_time"></el-option>
            <el-option label="审核日期" value="sumit_time"></el-option>
            <el-option label="工厂确认出货时间" value="pre_factory_time"></el-option>
            <el-option label="计划预计下单时间" value="expect_order_time"></el-option>
            <el-option label="预计交期" value="pre_delivery_time"></el-option>
          </el-select>
          <VDate v-model="form.dateRange.value" />
        </VGroup>
        <teleport v-if="demandTabsLoad" to="#demand-tabs">
          <el-tabs v-model="form.type" @tab-change="tabChange">
            <el-tab-pane v-for="(d, i) in options.type" :key="i" :label="`${d.label}(${d.num || 0})`" :name="d.value"></el-tab-pane>
          </el-tabs>
        </teleport>
      </template>
      <template #toolbar_btns>
        <div id="demand-tabs" class="w-full" v-dom-load="() => demandTabsLoad = true"></div>
        <VButton auth="/purchase/demand/plan/add" type="primary" @click="create(tableRef)"><i class="dk-iconfont icon-Plus" />添加</VButton>
        <VButton auth="/purchase/demand/plan/generate_demand_list" type="primary" @click="handleBatch('demand')"><svg-icon icon-class="sc" class="mr-1"
            style="font-size: 15px" />生成备库需求单</VButton>
        <VButton auth="/purchase/demand/plan/generate_material_preparation_contract" type="primary" @click="handleBatch('contract')">
          <svg-icon icon-class="sc" class="mr-1" style="font-size: 15px" />生成备料合同
        </VButton>
        <VAudit :auth="(options.batch_audit_status || []).map(d => d.auth)" field="pp_id" url="/v1/purchase-plan/purchase-plan/batch-audit" :tableRef="tableRef"
          :list="options.batch_audit_status" @success="query" />
        <VButton auth="/purchase/demand/plan/accept" :loading="loadings.receive" @click="handleBatch('receive')">采购接收
        </VButton>
        <VButton auth="/purchase/demand/plan/audit" :loading="loadings.audit" @click="handleBatch('audit')">批量反审</VButton>
        <VButton auth="/purchase/demand/plan/submit" :loading="loadings.submit" @click="handleBatch('submit')">批量提交
        </VButton>
        <VButton auth="/purchase/demand/plan/back" :loading="loadings.back" @click="handleBatch('back')">批量退回
        </VButton>
        <VButton auth="/purchase/demand/plan/batch" :loading="loadings.batch" @click="handleBatch('batch')">批量修改
        </VButton>
        <VButton auth="/purchase/demand/plan/void" :loading="loadings.cancel" @click="handleBatch('cancel')">批量作废
        </VButton>
        <VButton auth="/purchase/demand/plan/plan" @click="handleBatch('plan')">计划批量修改</VButton>
        <VButton auth="/purchase/demand/plan/purchase" @click="handleBatch('purchase')">采购批量修改</VButton>
        <VButton auth="/purchase/demand/plan/finish" @click="handleBatch('finish')">手动完结</VButton>
        <VImport auth="/purchase/demand/plan/import" :tableRef="tableRef" importUrl="/v1/purchase-plan/purchase-plan/import"
          downloadUrl="https://xc-pms.oss-cn-shenzhen.aliyuncs.com/template/purchase_plan.xlsx" class="ml-auto" @success="query" />
        <VAuth auth="/purchase/demand/plan/derive">
          <el-dropdown trigger="click" @command="handleExport">
            <el-button>
              <i class="dk-iconfont icon-Upload" />
              导出
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="purchase_plan">备料维度导出</el-dropdown-item>
                <el-dropdown-item command="purchase_plan_pr">PR维度导出</el-dropdown-item>
                <el-dropdown-item command="purchase_plan_po">PO维度导出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
          <VExport class="absolute -right-full" ref="exportRef" mainKey="pp_id" :name="exportName" :tableRef="tableRef" task />
        </VAuth>
      </template>
    </VTable>
    <Add ref="addRef" :options="options" @success="query" />
    <Receive ref="receiveRef" :options="options" @success="query" />
    <Batch ref="batchRef" @success="query" />
    <VLog ref="logRef" />
    <Contract ref="contractRef" :options="options" @success="query" />
    <PlanSetting ref="planSettingRef" :options="options" @success="query" />
  </VPage>
</template>
  
<script setup lang="jsx" name="Account">
import api from './api'
import Add from './components/Add'
import Receive from './components/Receive'
import Contract from './components/Contract'
import Batch from './components/Batch'
import VImport from '@/components/VImport'
import { getOption } from '@/api/public'
import PlanSetting from './components/planSetting'
import { ElMessage, ElMessageBox } from 'element-plus'
import { resSkip, resMessage} from "@/utils/res"
const router = useRouter()

const baseName = 'purchase_plan'
const tableRef = ref()
const logRef = ref()
const demandTabsLoad = ref(false)
const loadings = reactive({})
// 查询
const query = () => {
  tableRef?.value.query()
}

// 新增
const addRef = ref()
const create = () => {
  console.log('addRef', addRef)
  addRef?.value.open()
}


// 获取options
const options = ref({})
Promise.all([
  getOption({ type: 'purchase' }).then(res => res.data),
  getOption({ type: baseName }).then(res => res.data),
  Promise.resolve({ test: [{ label: '没有数据', value: 1 }] })
]).then(res => {
  options.value = res.reduce((acc, cur) => (((acc = { ...acc, ...cur }), acc)), {})
  getTotal()
})

// 导出
const exportName = ref('purchase_demand_sku')
const exportRef = ref()
const handleExport = (val) => {
  exportName.value = val
  nextTick(() => {
    exportRef?.value.open()
  })
}

// 获取状态数量
const getTotal = async (params = { type: 0 }) => {
  if (!options.value.type) return
  await api.total(params).then(res => {
    options.value.type.forEach(t => {
      t.num = res.data.find(d => d.value === t.value)?.num || 0
    })
  })
}
const tabValue = ref(0)
const tabChange = name => {
  tabValue.value = name
  query()
}

// 批量操作
const contractRef = ref()
const planSettingRef = ref()
const batchRef = ref()
const handleBatch = (name) => {
  const selection = tableRef.value.$table.getCheckboxRecords()
  if (!selection.length) {
    ElMessage.warning('请先选择行')
    return
  }
  const pp_ids = selection.map(d => d.pp_id)
  const showLoading = () => loadings[name] = true
  const closeLoading = () => loadings[name] = false
  const handle = {
    batch: () => {
      batchRef?.value.open(pp_ids)
    },
    back: () => {
      showLoading()
      api.back({pp_ids}).then(res => {
        ElMessage({ type: 'success', message: '操作成功', })
        query()
      }).finally(() => {
        closeLoading()
      })
    },
    demand: (is_skip = 1) => {
      showLoading()
      api.demand({ pp_ids, is_skip }).then(res => {
        resSkip({
          res,
          closeLoading,
          success: query,
          submit: () => {
            handle.demand(2)
          },
        })
      }).finally(closeLoading)
    },
    contract: () => {
      contractRef?.value.open(pp_ids)
    },
    receive: () => {
      receiveRef.value?.open(selection)
    },
    submit: () => {
      showLoading()
      api.batchSubmit({ pp_ids }).then(res => {
        ElMessage({ type: 'success', message: '操作成功', })
        query()
      }).finally(() => {
        closeLoading()
      })
    },
    audit: () => {
      ElMessageBox.prompt('反审原因', '反审', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showInput: false,
        customClass:'purchase-msg-box',
        message: () => <div>
          <VText value='反审原因' class='mb-1' />
          <VSelect v-model={select.value} placeholder="请选择" options={options.value.reason} clearable={false} class="w-full" select/>
        </div>
      })
        .then(({ value }) => {
          showLoading()
          api.batchAuditBack({ pp_ids, audit_remark: value }).then(res => {
            ElMessage({ type: 'success', message: '操作成功', })
            query()
          }).finally(() => {
            closeLoading()
          })
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '取消操作',
          })
        })
    },
    plan: () => {
      planSettingRef.value.open({pp_ids,name:'plan'})
    },
    purchase: () => {
      planSettingRef.value.open({pp_ids,name:'purchase'})
    },
    cancel: () => {
     ElMessageBox.confirm("是否确认作废？", "作废", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          showLoading()
          api.batch_cancel({ pp_ids}).then(res => {
            resMessage({
              res,
              success: () => {
                ElMessage.success("操作成功");
                query();
              },
            });
          }).finally(() => {
            closeLoading()
          })
        })
        .catch(() => {
          ElMessage({ type: "info", message: "取消操作" });
        });
    },
    finish: () => {
     ElMessageBox.confirm("是否确认手动完结？", "手动完结", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          showLoading()
          api.batch_finish({ pp_ids}).then(res => {
            resMessage({
              res,
              success: () => {
                ElMessage.success("操作成功");
                query();
              },
            });
          }).finally(() => {
            closeLoading()
          })
        })
        .catch(() => {
          ElMessage({ type: "info", message: "取消操作" });
        });   
    }
  }
  handle[name]()
}
// 单行操作
const receiveRef = ref()
const select = ref()
const handleRow = (name, row, rowIndex) => {
  const { pp_id } = row
  const showLoading = () => {
    loadings[name + rowIndex] = true
  }
  const closeLoading = () => {
    loadings[name + rowIndex] = false
  }
  const handle = {
    edit: () => {
      addRef?.value.open(pp_id)
    },
    change: () => {
      addRef?.value.open(pp_id, true)
    },
    submit: () => {
      showLoading()
      api.batchSubmit({ pp_ids: [pp_id] }).then(res => {
        ElMessage({ type: 'success', message: '操作成功', })
        query()
      }).finally(() => {
        closeLoading()
      })
    },
    audit: () => {
      const reason = options.value.reason || []
      select.value = reason[0]?.value
      ElMessageBox.prompt('反审原因', '反审', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showInput: false,
        customClass:'purchase-msg-box',
        message: () => <div>
          <div class='mb-2 text-gray-900'>反审原因</div>
          <VSelect v-model={select.value} placeholder="请选择" options={reason} clearable={false} class="w-full" select/>
        </div>
      })
        .then(({ value }) => {
          showLoading()
          api.batchAuditBack({ pp_ids: [pp_id], audit_remark: value }).then(res => {
            ElMessage({ type: 'success', message: '操作成功', })
            query()
          }).finally(() => {
            closeLoading()
          })
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '取消操作',
          })
        })
    },
    cancel: () => {
      ElMessageBox.confirm('是否确认作废？', '作废', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      })
        .then(() => {
          api.batch_cancel({ pp_ids:[pp_id] }).then(res => {
            resMessage({
              res,
              success: () => {
                ElMessage.success("操作成功");
                query();
              },
            });
          })
        })
        .catch(() => {
          ElMessage({ type: 'info', message: '取消操作', })
        })
    },
    finish: () => {
      ElMessageBox.confirm('是否确认手动完结？', '手动完结', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      })
        .then(() => {
          api.batch_finish({ pp_ids:[pp_id] }).then(res => {
            resMessage({
              res,
              success: () => {
                ElMessage.success("操作成功");
                query();
              },
            });
          })
        })
        .catch(() => {
          ElMessage({ type: 'info', message: '取消操作', })
        })  
    },
    back: () => {
      ElMessageBox.confirm('是否确认该备料单退回到待提交状态？', '退回', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      })
        .then(() => {
          api.change_status({ pp_id, type: 'back' }).then(res => {
            ElMessage({ type: 'success', message: '操作成功', })
            query()
          })
        })
        .catch(() => {
          ElMessage({ type: 'info', message: '取消操作', })
        })
    },
    receive: () => {
      receiveRef.value?.open([row])
    },
    toPath: () => {
      sessionStorage.setItem('_table_form', JSON.stringify({ tableForm: { skuInfo: { type: 'sku', value: row.sku } } }))
      router.push('/product/product')
    },
    log: () => {
      logRef?.value.open({ log_type: baseName, pp_id })
    }
  }
  handle[name]()
}

// 列表配置
const tableOptions = reactive({
  rowConfig: { height: 152 },
  showHeaderOverflow: false,
  formConfig: {
    data: {
      type: 0,
      leave_num: {},
      sku: { type: 'sku', value: '' }, // 组合
      new: { type: 'is_new', value: '' }, // 组合
      input: { type: 'plan_no', value: '' }, // 组合
      supplier: { type: 'supplier_id', value: '' }, // 组合
      dateRange: { type: 'create_time', value: [], range: true }, // 组合
    }
  },
  columns: [
    { type: 'checkbox', fixed: 'left', width: 40 },
    {
      title: '备料单号', minWidth: 166, slots: {
        default: ({ row }) => <VText value={row.plan_no} copy />
      }
    },
    {
      title: '备料需求信息', field: 'plan_type_name', minWidth: 200, slots: {
        default: ({ row }) => <div>
          <VText title='备料类型' value={row.plan_type_name} />
          <VText title='平台' value={row.platform_name} />
          <VText title='国家' value={row.country_name} />
          <VText title='销售人员' value={row.sale_name} />
          <VText title='部门' value={row.department_name} />
        </div>
      }
    },
    {
      title: '备料状态', field: 'plan_status_name', minWidth: 130, slots: {
        default: ({ row }) => <div>
          {row.is_change === 2 ? <el-tag type='warning' effect="dark" class='mb-1'>变更</el-tag> : ''}
          <VText value={row.plan_status_name} />
          </div>
      }
    },
    {
      title: '产品图片', field: 'plan_status_name', minWidth: 100, slots: {
        default: ({ row }) => <VImage src={row.main_img} size="80px"></VImage>
      }
    },
    {
      field: '', minWidth: 220, slots: {
        header: () => <div>
          <div>sku信息</div>
        </div>,
        default: ({ row }) => <div>
          <VText title='sku' value={row.sku} type='link' onClick={() => handleRow('toPath', row)}>
            <el-link type='primary'>{row.sku}</el-link>
            {row.is_new === 2 ? <el-tag type="success" effect="dark" size='small' class='mx-1'>新</el-tag> : ''}
            {row.is_special_purchase === 2 ? <el-tag type="danger" size='small' effect="dark">特</el-tag> : ''}
            {row.is_dangerous === 2 ? <svg t="1731396340885" class="-mt-[1px]" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7755" width="24" height="24"><path d="M426.14784 160.23552l-368.27136 628.736a102.4 102.4 0 0 0 88.35072 154.17344h736.54272a102.4 102.4 0 0 0 88.35072-154.17344l-368.2304-628.736a102.4 102.4 0 0 0-176.7424 0z" fill="#FADE14" p-id="7756"></path><path d="M545.54624 158.96576a61.44 61.44 0 0 1 21.95456 21.99552l368.27136 628.736a61.44 61.44 0 0 1-53.00224 92.48768H146.2272a61.44 61.44 0 0 1-53.00224-92.48768l368.2304-628.736a61.44 61.44 0 0 1 84.09088-21.99552z m-63.73376 28.34432l-2.6624 3.97312-368.27136 628.736a40.96 40.96 0 0 0 30.55616 61.44l4.79232 0.24576h736.54272a40.96 40.96 0 0 0 37.51936-57.42592l-2.17088-4.25984-368.27136-628.736a40.96 40.96 0 0 0-68.03456-3.97312z" fill="#333333" p-id="7757"></path><path d="M450.1504 397.47584a385.26976 385.26976 0 0 1-123.2896 123.2896l19.6608 23.7568c12.288-8.6016 24.576-17.6128 36.864-27.4432v85.1968c-1.6384 67.1744-20.48 119.1936-56.5248 156.8768l23.7568 21.2992c39.3216-44.6464 60.2112-104.0384 62.2592-178.176v-66.7648h277.2992v-28.672h-121.6512c16.384-13.9264 33.1776-31.1296 49.9712-50.7904v-21.2992h-158.1056c6.9632-9.8304 13.5168-20.0704 20.0704-30.72l-30.3104-6.5536z m79.872 109.3632H395.6736c15.9744-14.336 31.1296-29.9008 45.4656-46.6944h138.8544c-16.384 17.6128-33.1776 33.1776-49.9712 46.6944z m73.728 101.5808v38.0928c0 13.1072-5.3248 19.6608-15.9744 19.6608-14.7456 0-31.1296-0.8192-49.152-2.4576l6.9632 27.8528c17.2032 0.8192 34.816 1.2288 52.8384 1.2288 23.3472-0.8192 35.2256-13.5168 35.2256-37.2736v-73.3184h-169.1648v153.6c0 24.576 13.1072 36.864 40.1408 36.864h129.4336c23.3472 0 39.3216-3.6864 47.5136-10.24 9.0112-8.192 15.1552-30.72 18.8416-66.7648l-28.2624-9.0112c-2.4576 23.7568-5.7344 39.7312-10.6496 47.9232-4.5056 6.9632-16.384 10.6496-35.2256 10.6496h-109.7728c-14.7456 0-22.1184-6.5536-22.1184-19.2512v-117.5552h109.3632z" fill="#333333" p-id="7758"></path></svg> : ''}
          </VText>
          <VText title='供应商名称' value={row.supplier_name} />
          <VText title='供应商代码' value={row.supplier_code} />
          <VText title='采购人员' value={row.purchase_user_name} />
          <VText title='备料后采购天数' value={row.prepare_purchase_day} />
          <VText title='是否组合sku' value={row.is_combination_name} />
          <VText title='供应商负责人' value={row.director_id_name} />
        </div>
      }
    },
    {
      title: '产品名称', minWidth: 160, slots: {
        default: ({ row }) => <VText value={row.sku_name} line={3} />
      }
    },
    {
      title: '备料数量/金额信息', field: 'plan_num', minWidth: 160, slots: {
        default: ({ row }) => <div>
          <VText title='备料数量' value={row.plan_num} />
          <VText title='单价' value={row.purchase_untaxed_price} />
          <VText title='总金额' value={row.total_price} />
        </div>
      }
    },
    { title: '预计交期', field: 'pre_delivery_time', minWidth: 120 },
    {
      field: '', minWidth: 200, slots: {
        header: () => <div>ASIN<br/>国家<br/>目的仓</div>,
        default: ({ row }) => <div>
          <VText value={row.asin} />
          <VText value={row.country_name} />
          <VText value={row.destination_warehouse_name} />
        </div>
      }
    },
    { title: '运输方式', field: 'transport_type_name', minWidth: 120 },
    {
      title: 'PR/PO/余料数', field: 'demand_num', minWidth: 120, slots: {
        default: ({ row }) => {
          return <div>
            <VText title='已下PR数' value={row.demand_num} />
            <VText title='已下PO数' value={row.purchase_num} />
            <VText title='余料数' value={row.leave_num} />
          </div>
        }
      }
    },
    { title: '计划预计下单时间', field: 'expect_order_time', minWidth: 140 },
    {
      title: '关联PR', field: 'demand_list', minWidth: 140, slots: {
        default: ({ row, rowIndex }) => <VMore key={rowIndex}>
          {{
            default: (line) => (row.demand_list || []).map(d => <div class='w-full'>
              <VText value={d.demand_no} line={line} />
              <VText title='数量' value={d.demand_num} line={line} />
            </div>)
          }}
        </VMore>
      }
    },
    {
      title: '关联PO', field: 'purchase_list', minWidth: 140, slots: {
        default: ({ row, rowIndex }) => <VMore key={rowIndex}>
          {{
            default: (line) => (row.purchase_list || []).map(d => <div class='w-full'>
              <VText value={d.purchase_no} line={line} />
              <VText title='数量' value={d.purchase_num} line={line} />
            </div>)
          }}
        </VMore>
      }
    },
    {
      title: '备注', field: 'remark', minWidth: 120, slots: {
        default: ({ row }) => <VText value={row.remark} line={3} />
      }
    },
    {
      field: '', minWidth: 140, slots: {
        header: () => {
          return <div>
            <div>预计到货时间</div>
            <div>计划预计下单时间</div>
          </div>
        },
        default: ({ row }) => {
          const { pre_factory_time, expect_order_time } = row
          return <div>
            <VText value={pre_factory_time} />
            <VText value={expect_order_time} />
          </div>
        }
      }
    },
    {
      title: '备料合同', field: 'contract_no', minWidth: 180, slots: {
        default: ({ row }) => {
          return <VText value={row.contract_no} />
        }
      }
    },
    {
      title: '申请信息', field: 'create_user', minWidth: 230, slots: {
        default: ({ row }) => {
          const { create_time, create_user } = row
          return <div>
            <VText title='申请人' value={create_user} />
            <VText title='申请时间' value={create_time} />
          </div>
        }
      }
    },
    {
      title: '审核信息', field: '', minWidth: 230, slots: {
        default: ({ row }) => {
          const { audit_user, audit_time } = row
          return <div>
            <VText title='审核人' value={audit_user} />
            <VText title='审核时间' value={audit_time} />
          </div>
        }
      }
    },
    {
      title: '操作', width: 130, fixed: 'right', slots: {
        default: ({ row, rowIndex }) => {
          const { pp_id, plan_status } = row
          // plan_status 备料状态1待提交2待计划经理审核3待供应链总监审核4待运营经理审核5待采购接收6待计划确认7已完成8已驳回9已作废10待生成单据11部分生成单据12手动完结
          return [<div class='nm flex flex-wrap gap-x-2 gap-y-1'>
            {[1, 8].includes(plan_status) ? <VButton auth="/purchase/demand/plan/edit" link type="primary" onClick={() => handleRow('edit', row)}>编辑</VButton> : ''}
            {[10].includes(plan_status) ? <VButton auth="/purchase/demand/plan/audit" link type="primary" loading={loadings['audit' + rowIndex]} onClick={() => handleRow('audit', row, rowIndex)}>反审</VButton> : ''}
            {[1, 8].includes(plan_status) ? <VButton auth="/purchase/demand/plan/submit" link type="primary" loading={loadings['submit' + rowIndex]} onClick={() => handleRow('submit', row, rowIndex)}>提交</VButton> : ''}
            {[1, 8].includes(plan_status) ? <VButton auth="/purchase/demand/plan/void" link type="primary" onClick={() => handleRow('cancel', row)}>作废</VButton> : ''}
            {[5].includes(plan_status) ? <VButton auth="/purchase/demand/plan/accept" link type="primary" onClick={() => handleRow('receive', row)}>采购接收</VButton> : ''}
            {[10, 11].includes(plan_status) ? <VButton auth="/purchase/demand/plan/change" link type="primary" onClick={() => handleRow('change', row)}>变更</VButton> : ''}
            {[10, 11].includes(plan_status) ? <VButton auth="/purchase/demand/plan/finish" link type="primary" onClick={() => handleRow('finish', row)}>手动完结</VButton> : ''}
            {/* {[6, 7].includes(plan_status) ? <VButton auth="/purchase/demand/plan/add" link type="primary" onClick={() => handleRow('back', row)}>退回</VButton> : ''} */}
            <VButton auth="/purchase/demand/plan/logs" link type="primary" onClick={() => handleRow('log', row)}>日志</VButton>
          </div>]
        }
      }
    }
  ],
  proxyConfig: {
    autoLoad: false,
    ajax: {
      query: async ({ page, form }) => {
        await getTotal({ ...form, ...page })
        return api.query({ ...form, ...page }).then(res => {
          options.value.type.forEach(d => {
            if (d.value === tabValue.value) {
              d.num = res.total
            }
          })
          return res
        })
      }
    }
  }
})
</script>
  